﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	Linea_dlg
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Map_Fvg
{
    public partial class MyControlPoint : UserControl, IDragDropEnabled
    {
        private int codice1;
        private Color MyTextColor;
        private Font MyFont;
        private float angoloT;
        private string fileImagX;

        private string Stx;

        public MyControlPoint()
        {
            InitializeComponent();
            MyFont = this.Font;
            MyTextColor = Color.Black;
        }
        //         private void MyControlTesto_Paint(object sender, PaintEventArgs e)
        protected override void OnPaint(PaintEventArgs e)
        {
            if (!this.IsDragging)
            {
                if (ViewQ.Select_flag == (int)ViewQ.flagSelez.punto)
                {
                    Graphics dc = e.Graphics;
           
    //                dc.RotateTransform(ViewQ.alpha);
                    Pen blackPen = new Pen(Color.Red, 1);
                    bool bAngle = false;
                    /*
                    if (codice1 == 1)
                    {


                        Rectangle pntR = new Rectangle(Pnt.X, Pnt.Y, 4, 4);
                        dc.DrawEllipse(blackPen, pntR);

                        return;
                    }
                    Font iFont = MyFont;

                    float width1 = dc.MeasureString(Stx, iFont).Width + 10;
                    float height1 = dc.MeasureString(Stx, iFont).Height;
                    height1 = width1;
                    //           Rectangle drawRect = new Rectangle(1, 1, this.ClientSize.Width - 2, this.ClientSize.Height - 2);
                    //         dc.DrawRectangle(blackPen, drawRect);

*/
   //                 MessageBox.Show(ClientSize.ToString());

                    Point[] polygon = new Point[5];
                    polygon[0].X = 1;
                    polygon[0].Y = 1;
                    polygon[1].X = ClientSize.Width - 2;
                    polygon[1].Y = 1;
                    polygon[2].X = ClientSize.Width - 2;
                    polygon[2].Y = ClientSize.Height - 2;
                    polygon[3].X = 1;
                    polygon[3].Y = ClientSize.Height - 2;
                    polygon[4] = polygon[0];
                    dc.DrawLines(blackPen, polygon);

                    Rectangle RectSelect = new Rectangle(0, 0, 3, 3);
                    RectSelect.X = polygon[0].X;
                    RectSelect.Y = polygon[0].Y; // +RectSelect.Height;
                    dc.DrawRectangle(blackPen, RectSelect);
                    SolidBrush blackBrush = new SolidBrush(Color.Black);
                    dc.FillRectangle(blackBrush, RectSelect);

                    RectSelect.X = polygon[1].X - RectSelect.Width;
                    RectSelect.Y = polygon[1].Y;
                    dc.DrawRectangle(blackPen, RectSelect);
                    dc.FillRectangle(blackBrush, RectSelect);

                    RectSelect.X = polygon[2].X - RectSelect.Width;
                    RectSelect.Y = polygon[2].Y - RectSelect.Height;
                    dc.DrawRectangle(blackPen, RectSelect);
                    dc.FillRectangle(blackBrush, RectSelect);

                    RectSelect.X = polygon[3].X;
                    RectSelect.Y = polygon[3].Y - RectSelect.Height;
                    dc.DrawRectangle(blackPen, RectSelect);
                    dc.FillRectangle(blackBrush, RectSelect);
/*
                    width1 = dc.MeasureString(Stx, iFont).Width + 10;
                    height1 = dc.MeasureString(Stx, iFont).Height;
                    //    return;

                    if (angoloT != 0.0F)
                    {
                        bAngle = true;
                    }       
      */                             
                    //          Pen iPen = new Pen(icolor, width);
/*
                                        Point Pnt = new Point(0, 0);

                                        SizeF stringSize = new SizeF();
                                        stringSize = dc.MeasureString(Stx, iFont);

                                        if (bAngle)
                                        {
                                            RectangleF drawRect = new RectangleF(0, 0, width1, height1);
                                            Rectangle pntR = new Rectangle(0, 0, 2, 2);
                                            dc.TranslateTransform(Pnt.X, Pnt.Y);
                                            dc.RotateTransform(angoloT);


                                            dc.DrawEllipse(blackPen, pntR);
                                            dc.DrawString(Stx, iFont, iBrush, drawRect);
                                            dc.ResetTransform();
                                        }
                                        else
                                        {
                                            RectangleF drawRect = new RectangleF(Pnt.X, Pnt.Y, width1, height1);
                                            Rectangle pntR = new Rectangle(Pnt.X, Pnt.Y, 2, 2);
                                            dc.DrawEllipse(blackPen, pntR);
                                            dc.DrawString(Stx, iFont, iBrush, drawRect);


                                            int height = this.ClientSize.Height / 2 - (int)(height1 / 2.0F);
                                            Rectangle drawRect1 = new Rectangle(0, height, (int)width1, (int)height1);

                                            dc.DrawString(Stx, iFont, iBrush, drawRect1);

                                        }
                                        dc.ResetTransform();

                    */
                    Point Pnt = new Point(0, 0);
                    char[] delimiterChars0 = { ',', '/' };
                    string[] words;

                    if (codice == 0) return;
  //                  MessageBox.Show("Mycodice " + codice1.ToString());
                    if (codice == 1)
                    {
                        Rectangle pntR = new Rectangle(Pnt.X, Pnt.Y, 3, 3);
                        dc.DrawEllipse(blackPen, pntR);
                        return;
                    }

//                    if (codice != 1)
                    {
                        int width = 0;
                        int height = 0;
         //               int inclin = 0;
           //             int spess = 0;

                        string fileX = fileImagX;

                        string ss = string.Empty;
                        switch (codice)
                        {
                            case 5:         //  sorgente perenne
                            case 38:        //  pino mugo
                            case 39:        // faggio
                                int ipos = ss.IndexOf('/');
                                string s1 = ss.Substring(0, ipos);
                                words = s1.Split(delimiterChars0);
                                width = int.Parse(words[0]);
                                height = int.Parse(words[1]);
                                break;
                        }

                        Image newImage = Image.FromFile(fileX);
                        if (bAngle)
                        {
                            dc.TranslateTransform(Pnt.X, Pnt.Y);
                            dc.RotateTransform(angoloT);
                            Rectangle destRect = new Rectangle(0, 0, width, height);
                            dc.DrawImage(newImage, destRect);
                            dc.ResetTransform();
                            dc.RotateTransform(ViewQ.alpha);
                        }
                        else
                        {
                            Rectangle destRect = new Rectangle(Pnt.X, Pnt.Y, width, height);
                            dc.DrawImage(newImage, destRect);
                        }
                    }

  //                  dc.ResetTransform();

                    base.OnPaint(e);
                }
            }
        }

        public float angolo
        {
            get
            {
                return angoloT;
            }
            set
            {
                angoloT = value;
            }
        }

        public string testo
        {
            get
            {
                return Stx;
            }
            set
            {
                if (value == null) return;
                Stx = string.Copy(value);

            }
        }

        public Color colore
        {
            get
            {
                return MyTextColor;
            }
            set
            {
                if (value != null) MyTextColor = value;
            }
        }

        public int codice
        {
            get
            {
                return codice1;
            }
            set
            {
                if (value != 0) codice1 = value;
            }
        }

        public Font myFont
        {
            get
            {
                return MyFont;
            }
            set
            {
                if (value != null) MyFont = value;
            }
        }

        public string fileImag
        {
            get
            {
                return fileImagX;
            }
            set
            {
                if (value != null) fileImagX = value;
            }
        }

        bool m_IsDragging = false;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        //	private System.ComponentModel.Container components = null;

        /// <summary>
        /// Gets / Sets if the control is being dragged
        /// </summary>
        public bool IsDragging
        {
            get { return m_IsDragging; }
            set { m_IsDragging = value; }
        }

        /// <summary>
        /// Unused but in the interface
        /// </summary>
        public void StoreLocation()
        {


        }

        /// <summary>
        /// Unused but in the interface
        /// </summary>
        /// <param name="_successfull">unused</param>
        public void DropComplete(bool _successfull)
        {

        }

        public void completedDrag(int X, int Y)
        {
            Point Hit = new Point();
            Point Hit1 = new Point();

            double angleRad = -ViewQ.alpha * Math.PI / 180.0D; // 0F;

            Hit1.X = (int)((double)X * Math.Cos(angleRad) - (double)Y * Math.Sin(angleRad)) - ViewQ.Xoff;
            Hit1.Y = (int)((double)X * Math.Sin(angleRad) + (double)Y * Math.Cos(angleRad)) - ViewQ.Yoff;

            Hit.X = (int)(Hit1.X / ViewQ.scalaX) + ViewQ.Xmin;
            Hit.Y = (int)(Hit1.Y / ViewQ.scalaY) + ViewQ.Ymin;
            ViewQ.vectShapesP[ViewQ.i_selected].XX.X = Hit.X;
            ViewQ.vectShapesP[ViewQ.i_selected].XX.Y = Hit.Y;
            ViewQ.vectShapesP[ViewQ.i_selected].modifica = 1;
            Save.modificaPunto = 1;

        }
    }
}
